#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "inituserwin.h"
#include "showtipwin.h"
#include <QDate>
#include <QTime>
#include <QMessageBox>
#include <QTimerEvent>
#include <QDebug>
#include <QMediaPlaylist>

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	userType = 0;
	if (userType == 0)
	{
		InitUserWin iuw;
		int uType = iuw.exec();
		if (uType != 0)
		{
			userType = uType;
		}
		else
		{
			QMessageBox::critical(this, "错误", "没有设置用户类型，程序退出！");
			qApp->quit();
			this->close();
			quitTimer = this->startTimer(100);
			return;
		}
	}

	lastShowTime = 0;
	mainTimer = this->startTimer(60 * 1000);
//	this->setVisible(false);
	this->createActions();
	QUrl u = QUrl::fromLocalFile(/*":/mp3/alarm"*/ "/home/yanjingtu/下载/alarm.mp3");
	QMediaContent mc = QMediaContent(u);
//	QMediaPlaylist* mlist = new QMediaPlaylist();
//	mlist->addMedia(mc);
	player = new QMediaPlayer();
//	player->setPlaylist(mlist);
	player->setMedia(mc);
	player->setVolume(100);
	player->play();
}

MainWindow::~MainWindow()
{
	delete ui;
}

void MainWindow::timerEvent(QTimerEvent* event)
{
	int timerId = event->timerId();
	qDebug() << "Timer ID:" << timerId;

	if (timerId == mainTimer)
	{
		QDate dt = QDate::currentDate();
		QTime tm = QTime::currentTime();
		int weekday = dt.dayOfWeek();
		int hour = tm.hour();
		int minute = tm.minute();
		if (weekday >= 1 && weekday <= 5)
		{
			if (hour == 17)
			{
				if (minute >= 3 && minute <= 58)
				{
					showTipWin();
				}
			}
		}
		else if (weekday == 6)
		{
			if (hour == 16)
			{
				if (minute >= 3 && minute <= 28)
				{
					showTipWin();
				}
			}
		}
	}
	else if (timerId == quitTimer)
	{
		qApp->quit();
		this->close();
	}
}

void MainWindow::showTipWin()
{
	if (QDateTime::currentDateTime().toTime_t() - lastShowTime > 60 * 60)
	{
		ShowTipWin stw;
		stw.exec();
		lastShowTime = QDateTime::currentDateTime().toTime_t();
	}
}

void MainWindow::createActions()
{
	//创建托盘项
	action_quit = new QAction(this);
	action_help = new QAction(this);
	action_about = new QAction(this);
	action_order = new QAction(this);

	//设置托盘项图标
	action_quit->setIcon(QIcon(":/icon/quit"));
	action_help->setIcon(QIcon(":/icon/help"));
	action_about->setIcon(QIcon(":/icon/about"));
	action_order->setIcon(QIcon(":/icon/order"));

	//设置托盘项文本
	action_quit->setText(QString("退出"));
	action_help->setText(QString("帮助"));
	action_about->setText(QString("关于"));
	action_order->setText(QString("预订"));

	//添加菜单项
	trayIconMenu = new QMenu(this);
	trayIconMenu->addAction(action_order);
	trayIconMenu->addSeparator();
	trayIconMenu->addAction(action_help);
	trayIconMenu->addAction(action_about);
	trayIconMenu->addSeparator();
	trayIconMenu->addAction(action_quit);

	trayIcon = new QSystemTrayIcon(this);//创建一托盘图标
	trayIcon->setIcon(QIcon(":/icon/tray"));
	trayIcon->setToolTip("orderSys");
	trayIcon->setContextMenu(trayIconMenu);//设置托盘上下文菜单为trayIconMenu
	trayIcon->show();

	//设置信号连接（这里仅列举连接显示窗口的信号）
	QObject::connect(action_quit, SIGNAL(triggered()), qApp, SLOT(quit()));//若触发了退出就退出程序
	QObject::connect(action_help, SIGNAL(triggered()), this, SLOT(showHelp()));
	QObject::connect(action_about, SIGNAL(triggered()), this, SLOT(showAbout()));
	QObject::connect(action_order, SIGNAL(triggered()), this, SLOT(showOrder()));
}

void MainWindow::showHelp()
{
	QMessageBox::information(this, "提示", "没有帮助信息");
}

void MainWindow::showAbout()
{
	QMessageBox::information(this, "提示", "没有关于信息");
}

void MainWindow::showOrder()
{
	;
}
